package com.promise.jokerdream.database.entity

import androidx.room.Entity
import androidx.room.ForeignKey
import androidx.room.Index
import androidx.room.PrimaryKey
import com.promise.jokerdream.config.user.UserConfig
import com.promise.jokerdream.config.game.*
import com.promise.jokerdream.database.dao.*
import com.promise.jokerdream.model.formation.FormationSelection
import com.promise.jokerdream.model.currency.CurrencyExchangeAmount
import kotlinx.serialization.json.Json

/**
 * Room 实体类 - 用户配置主表
 * 关联到 UserAccountEntity
 */
@Entity(
    tableName = "user_config",
    foreignKeys = [
        ForeignKey(
            entity = UserAccountEntity::class,
            parentColumns = ["userId"],
            childColumns = ["userId"],
            onDelete = ForeignKey.CASCADE
        )
    ],
    indices = [Index(value = ["userId"], unique = true)]
)
data class UserConfigEntity(
    @PrimaryKey
    val userId: String,
    val lastHourlyWorkExecutedTime: String? = null
) {
    /**
     * 转换为 UserConfig 对象
     * 从27个独立的游戏配置表读取数据
     */
    suspend fun toUserConfig(
        constellationGameConfigDao: ConstellationGameConfigDao,
        abyssGameConfigDao: AbyssGameConfigDao,
        scratchGameConfigDao: ScratchGameConfigDao,
        mistyGameConfigDao: MistyGameConfigDao,
        penguinFightGameConfigDao: PenguinFightGameConfigDao,
        fightTowerGameConfigDao: FightTowerGameConfigDao,
        experienceGameConfigDao: ExperienceGameConfigDao,
        ascendHeavenGameConfigDao: AscendHeavenGameConfigDao,
        wulinGameConfigDao: WulinGameConfigDao,
        mercenaryDispatchGameConfigDao: MercenaryDispatchGameConfigDao,
        immortalsGameConfigDao: ImmortalsGameConfigDao,
        knightMissionGameConfigDao: KnightMissionGameConfigDao,
        worldTreeGameConfigDao: WorldTreeGameConfigDao,
        factionCommerceGameConfigDao: FactionCommerceGameConfigDao,
        knightGameConfigDao: KnightGameConfigDao,
        arenaGameConfigDao: ArenaGameConfigDao,
        sectGameConfigDao: SectGameConfigDao,
        escortGameConfigDao: EscortGameConfigDao,
        factionFeedDemoGameConfigDao: FactionFeedDemoGameConfigDao,
        otherGameConfigDao: OtherGameConfigDao,
        wlmzGameConfigDao: WlmzGameConfigDao,
        formationGameConfigDao: FormationGameConfigDao,
        knightDreamGameConfigDao: KnightDreamGameConfigDao,
        currencyExchangeGameConfigDao: CurrencyExchangeGameConfigDao,
        chaosWarGameConfigDao: ChaosWarGameConfigDao,
        sectTournamentGameConfigDao: SectTournamentGameConfigDao,
        knightArenaGameConfigDao: KnightArenaGameConfigDao,
        taskGroupConfigDao: TaskGroupConfigDao,
        taskConfigDao: TaskConfigDao,
        timerTaskConfigDao: TimerTaskConfigDao
    ): UserConfig {
        // 从27个独立的表加载游戏配置
        val constellationEntity = constellationGameConfigDao.getByUserId(userId)
        val abyssEntity = abyssGameConfigDao.getByUserId(userId)
        val scratchEntity = scratchGameConfigDao.getByUserId(userId)
        val mistyEntity = mistyGameConfigDao.getByUserId(userId)
        val penguinFightEntity = penguinFightGameConfigDao.getByUserId(userId)
        val fightTowerEntity = fightTowerGameConfigDao.getByUserId(userId)
        val experienceEntity = experienceGameConfigDao.getByUserId(userId)
        val ascendHeavenEntity = ascendHeavenGameConfigDao.getByUserId(userId)
        val wulinEntity = wulinGameConfigDao.getByUserId(userId)
        val mercenaryDispatchEntity = mercenaryDispatchGameConfigDao.getByUserId(userId)
        val immortalsEntity = immortalsGameConfigDao.getByUserId(userId)
        val knightMissionEntity = knightMissionGameConfigDao.getByUserId(userId)
        val worldTreeEntity = worldTreeGameConfigDao.getByUserId(userId)
        val factionCommerceEntity = factionCommerceGameConfigDao.getByUserId(userId)
        val knightEntity = knightGameConfigDao.getByUserId(userId)
        val arenaEntity = arenaGameConfigDao.getByUserId(userId)
        val sectEntity = sectGameConfigDao.getByUserId(userId)
        val escortEntity = escortGameConfigDao.getByUserId(userId)
        val factionFeedDemoEntity = factionFeedDemoGameConfigDao.getByUserId(userId)
        val otherEntity = otherGameConfigDao.getByUserId(userId)
        val wlmzEntity = wlmzGameConfigDao.getByUserId(userId)
        val formationEntity = formationGameConfigDao.getByUserId(userId)
        val knightDreamEntity = knightDreamGameConfigDao.getByUserId(userId)
        val currencyExchangeEntity = currencyExchangeGameConfigDao.getByUserId(userId)
        val chaosWarEntity = chaosWarGameConfigDao.getByUserId(userId)
        val sectTournamentEntity = sectTournamentGameConfigDao.getByUserId(userId)
        val knightArenaEntity = knightArenaGameConfigDao.getByUserId(userId)
        
        // 构建 UserGameConfig，如果配置为 null 则创建默认值
        val gameConfig = UserGameConfig(
            constellationConfig = constellationEntity ?: ConstellationGameConfigEntity(userId = userId),
            abyssConfig = abyssEntity ?: AbyssGameConfigEntity(userId = userId),
            scratchConfig = scratchEntity ?: ScratchGameConfigEntity(userId = userId),
            mistyConfig = mistyEntity ?: MistyGameConfigEntity(userId = userId),
            penguinFightConfig = penguinFightEntity ?: PenguinFightGameConfigEntity(userId = userId),
            fightTowerConfig = fightTowerEntity ?: FightTowerGameConfigEntity(userId = userId),
            experienceConfig = experienceEntity ?: ExperienceGameConfigEntity(userId = userId),
            ascendHeavenConfig = ascendHeavenEntity ?: AscendHeavenGameConfigEntity(userId = userId),
            wulinConfig = wulinEntity ?: WulinGameConfigEntity(userId = userId),
            mercenaryDispatchConfig = mercenaryDispatchEntity ?: MercenaryDispatchGameConfigEntity(userId = userId),
            immortalsConfig = immortalsEntity ?: ImmortalsGameConfigEntity(userId = userId),
            knightMissionConfig = knightMissionEntity ?: KnightMissionGameConfigEntity(userId = userId),
            worldTreeConfig = worldTreeEntity ?: WorldTreeGameConfigEntity(userId = userId),
            factionCommerceConfig = factionCommerceEntity ?: FactionCommerceGameConfigEntity(userId = userId),
            knightConfig = knightEntity ?: KnightGameConfigEntity(userId = userId),
            arenaConfig = arenaEntity ?: ArenaGameConfigEntity(userId = userId),
            sectConfig = sectEntity ?: SectGameConfigEntity(userId = userId),
            escortConfig = escortEntity ?: EscortGameConfigEntity(userId = userId),
            factionFeedDemoConfig = factionFeedDemoEntity ?: FactionFeedDemoGameConfigEntity(userId = userId),
            otherConfig = otherEntity ?: OtherGameConfigEntity(userId = userId),
            wlmzConfig = wlmzEntity ?: WlmzGameConfigEntity(userId = userId),
            formationConfig = formationEntity ?: FormationGameConfigEntity(userId = userId),
            knightDreamConfig = knightDreamEntity ?: KnightDreamGameConfigEntity(userId = userId),
            currencyExchangeConfig = currencyExchangeEntity ?: CurrencyExchangeGameConfigEntity(userId = userId),
            chaosWarConfig = chaosWarEntity ?: ChaosWarGameConfigEntity(userId = userId),
            sectTournamentConfig = sectTournamentEntity ?: SectTournamentGameConfigEntity(userId = userId),
            knightArenaConfig = knightArenaEntity ?: KnightArenaGameConfigEntity(userId = userId)
        )
        
        // 加载任务组配置
        val taskGroupEntities = taskGroupConfigDao.getByUserId(userId)
        
        // 加载定时任务配置
        val timerTaskEntities = timerTaskConfigDao.getByUserId(userId)
        
        return UserConfig(
            gameConfig = gameConfig,
            taskListConfig = taskGroupEntities,
            timerTaskConfig = timerTaskEntities,
            lastHourlyWorkExecutedTime = lastHourlyWorkExecutedTime
        )
    }
}

